<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-functools.html" />
<link rel="prev" href="module-random.html" />
<link rel="parent" href="numeric.html" />
<link rel="next" href="itertools-functions.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>6.5 itertools -- Functions creating iterators for efficient looping</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.4 random  "
  href="module-random.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="6. numeric and Mathematical"
  href="numeric.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="6.5.1 itertool functions"
  href="itertools-functions.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-random.html">6.4 random  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="numeric.html">6. Numeric and Mathematical</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="itertools-functions.html">6.5.1 Itertool functions</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION008500000000000000000">
6.5 <tt class="module">itertools</tt> --
         Functions creating iterators for efficient looping</a>
</h1>

<p>
<a name="module-itertools"></a>

<span class="versionnote">New in version 2.3.</span>

<p>
This module implements a number of iterator building blocks inspired
by constructs from the Haskell and SML programming languages.  Each
has been recast in a form suitable for Python.

<p>
The module standardizes a core set of fast, memory efficient tools
that are useful by themselves or in combination.  Standardization helps
avoid the readability and reliability problems which arise when many
different individuals create their own slightly varying implementations,
each with their own quirks and naming conventions.

<p>
The tools are designed to combine readily with one another.  This makes
it easy to construct more specialized tools succinctly and efficiently
in pure Python.

<p>
For instance, SML provides a tabulation tool: <code>tabulate(f)</code>
which produces a sequence <code>f(0), f(1), ...</code>.  This toolbox
provides <tt class="function">imap()</tt> and <tt class="function">count()</tt> which can be combined
to form <code>imap(f, count())</code> and produce an equivalent result.

<p>
Likewise, the functional tools are designed to work well with the
high-speed functions provided by the <tt class="module"><a href="module-operator.html">operator</a></tt> module.

<p>
The module author welcomes suggestions for other basic building blocks
to be added to future versions of the module.

<p>
Whether cast in pure python form or compiled code, tools that use iterators
are more memory efficient (and faster) than their list based counterparts.
Adopting the principles of just-in-time manufacturing, they create
data when and where needed instead of consuming memory with the
computer equivalent of ``inventory''.

<p>
The performance advantage of iterators becomes more acute as the number
of elements increases - at some point, lists grow large enough to
severely impact memory cache performance and start running slowly.

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <div class="seetext"><p>The Standard ML Basis Library,
           <em class="citetitle"><a
 href="http://www.standardml.org/Basis/"
 title="The Standard ML Basis Library"
 >The Standard ML Basis Library</a></em>.</p></div>

<p>
<div class="seetext"><p>Haskell, A Purely Functional Language,
           <em class="citetitle"><a
 href="http://www.haskell.org/definition/"
 title="Definition of Haskell and the Standard Libraries"
 >Definition of Haskell and the Standard Libraries</a></em>.</p></div>
</div>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="itertools-functions.html">6.5.1 Itertool functions</a>
<li><a href="itertools-example.html">6.5.2 Examples</a>
<li><a href="itertools-recipes.html">6.5.3 Recipes</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.4 random  "
  href="module-random.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="6. numeric and Mathematical"
  href="numeric.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="6.5.1 itertool functions"
  href="itertools-functions.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-random.html">6.4 random  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="numeric.html">6. Numeric and Mathematical</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="itertools-functions.html">6.5.1 Itertool functions</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
